package com.baosight.sczx.zm.cs.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cd.Documents;
import com.baosight.sczx.common.zm.cInterface.cs.Engineering;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.common.zm.cInterface.cs.SystemSettingCS;
import com.baosight.sczx.common.zm.cInterface.cs.SystemSettingCSNumber;
import com.baosight.sczx.zm.cs.domain.ZMCSWorkingFace;

/**
 * <p>Title: ServiceZMCS62.java</p>
 * <p>Description: 工程建模-工作面服务类</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 张小飞
 * @version 1.0
 * <pre>Histroy:
 *          2011-6-7 下午01:19:24  张小飞  Create
 *</pre>
 */
public class ServiceZMCS62 extends ServiceEPBase {
	
	/**
	 * 模块进入初始化方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS62</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-12上午08:25:20 严洪浩
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) 
	{
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.coalType", true));//查询区'采煤方法'
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.recoveryProcess", true));//新增 '回采工艺'
		
		outInfo.set("workingfaceNoType", SystemSettingCS.getInstance().getWorkingfaceNo());//工作面编号形式 0自动 1手动 2生成
		return outInfo;
	}
	
	/**
	 * 工作面数据查询方法
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCSWorkingFace</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-10上午11:33:58 路涛
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) 
	{
		String fdEngineeringDesignId = inInfo.getString("fdParentId");//工程设计ID
		StaticTools.setQueryParameters(inInfo, "fdEngineeringDesignId", fdEngineeringDesignId, true);//添加查询条件
		EiInfo outInfo = super.query(inInfo, "ZMCSWorkingFace.query", new ZMCSWorkingFace());

		return outInfo;
	}
	
	/**
	 * 工作面数据新增方法
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCSWorkingFace</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-10上午11:34:17 路涛
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) 
	{
		//校验编号、名称、简称是否重复
		String numberType = inInfo.getString("numberType");//编号形式0自动 1手动 2生成
		if(numberType != null && numberType.equals("0")) {
			SystemSettingCSNumber _cs = new SystemSettingCSNumber("1");
			String fdEngineeringdesignId = inInfo.getBlock("result").getCellStr(0, "fdEngineeringdesignId");
			String _temp = _cs.generateNewNumber(fdEngineeringdesignId);
			inInfo.getBlock("result").setCell(0, "fdNo", _temp);//编号
		}
		
		boolean validate = validate(inInfo, "ZMCSWorkingFace.validate", new HashMap<String, String>());
		
		if(validate)
		{
			return inInfo;
		}
		else 
		{
			String keyId = TablePk.getInstance().getNextValue("T_CI_WORKINGFACE");
			inInfo.getBlock("result").setCell(0, "fdKeyId", keyId);//主键
			EiInfo outinfo = super.insert(inInfo, "ZMCSWorkingFace.insert", "result");
			String title = inInfo.getBlock("result").getCell(0, "fdName").toString() + "设计文档";
			Documents.insertDoc(inInfo, keyId, title, "生产设计", "采煤", "");//添加附件
			return outinfo;
		}
	}
	
	/**
	 * 工作面数据修改方法
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCSWorkingFace</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-10上午11:34:26 路涛
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) 
	{
		//校验名称、简称是否重复
		Map<String, String> params = new HashMap<String, String>();
		params.put("fdKeyId", inInfo.getBlock("result").getCellStr(0, "fdKeyId"));
		
		boolean validate = validate(inInfo, "ZMCSWorkingFace.validate", params);
		if(validate)
		{
			return inInfo;
		}
		else
		{
			EiInfo outInfo = super.update(inInfo, "ZMCSWorkingFace.update");
			String title = inInfo.getBlock("result").getCell(0, "fdName").toString() + "设计文档";
			Documents.insertDoc(inInfo, inInfo.getBlock("result").getCellStr(0, "fdKeyId"), title, "生产设计", "采煤", "");//添加附件
			return outInfo;
		}
	}
	
	/**
	 * 工作面数据删除方法
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCSWorkingFace</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-10上午11:34:32 路涛
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) 
	{
		EiInfo outInfo = null;
		
		List<String> _list = new ArrayList<String>();
		Map<String,List<String>> params = new HashMap<String,List<String>>();

		for(int i = 0; i < inInfo.getBlock(EiConstant.resultBlock).getRowCount(); i++){
			_list.add(inInfo.getBlock("result").getCellStr(i, "fdKeyId"));
		}
		params.put("fdKeyIds", _list);
		
		//有无子节点校验
		boolean validate = Engineering.validateHasChildren(params, inInfo);
		if(validate) {
			return inInfo;
		}
		else {
			//删除附件信息
			for(int i=0; i<inInfo.getBlock("result").getRowCount(); i++)
			{
				Documents.deleteDoc(inInfo.getBlock("result").getCellStr(i, "fdKeyId"));
			}
			
			EiInfo _temp = super.delete(inInfo, "ZMCSWorkingFace.delete");
	        outInfo = this.query(inInfo);
	        outInfo.setMsg(_temp.getMsg());
	        outInfo.setDetailMsg(_temp.getDetailMsg());
		}
		
		return outInfo;
	}
	
	/**
	 * 获取最新的工作面编号
	 * <p>Function: getNewNumber</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-13上午06:28:28 严洪浩
	 * </p>
	 *
	 */
	public EiInfo getNewNumber(EiInfo inInfo)
	{
		String fdEngineeringdesignId = inInfo.getString("fdEngineeringdesignId");
		SystemSettingCSNumber _cs = new SystemSettingCSNumber("1");
		String _temp = _cs.generateNewNumber(fdEngineeringdesignId);
		inInfo.set("fdNo", _temp);
		return inInfo;
	}
	
	/**
	 * 校验编号名称简称不能重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-1-10下午01:50:00 路涛
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		
		String fdName = inInfo.getBlock("result").getCellStr(0, "fdName");
		String fdNo = inInfo.getBlock("result").getCellStr(0, "fdNo");
		String fdAbbreviation = inInfo.getBlock("result").getCellStr(0, "fdAbbreviation");
		
		params.put("fdName", fdName);
		params.put("fdNo", fdNo);
		params.put("fdAbbreviation", fdAbbreviation);
		
		List result = dao.query("ZMCSWorkingFace.validate", params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			ZMCSWorkingFace _temp = (ZMCSWorkingFace)result.get(0);
			
			StringBuffer msg = new StringBuffer("提交失败，");
			if(_temp.getFdNo() != null && _temp.getFdNo().equals(fdNo)){
				msg.append(" 编号 ");
			}
			if(_temp.getFdName() != null && _temp.getFdName().equals(fdName)) {
				msg.append(" 名称 ");
			}
			if(_temp.getFdAbbreviation() != null && _temp.getFdAbbreviation().equals(fdAbbreviation)) {
				msg.append(" 简称 ");
			}
			msg.append("重复！");
			
			inInfo.setMsg(msg.toString());
			return true;
		}
		return false;
	}
	
	/**
	 * 查询附件
	 * <p>Function: queryFiles</p>
	 * <p>Description: </p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-6-30上午10:38:30 张志芳
	 * </p>
	 *
	 */
	public EiInfo queryFiles(EiInfo inInfo) {
		String keyId = inInfo.getString("fdKeyId");
		return Documents.getFilesInfo("filesresult", keyId);
	}
}
